package model.proxy
{
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import ApplicationFacade;
	import model.Formation;
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy; 
	
	public class FormationProxy extends Proxy implements IProxy 
	{ 
		
		public static const NAME : String = 'FormationProxy';		
		public static const LISTE_FORMATIONS_OBTENUE : String = "ListeFormationsObtenue";
		
		private var remoteObject : RemoteObject = null;
		
		/** 
		 * Constructor 
		 * We create the table logins and insert a login by default to test 
		 */ 
		public function FormationProxy(data:Object=null) 
		{ 
			super(NAME, data);
			
			remoteObject = new RemoteObject();
			remoteObject.destination = "RemoteServiceFormation";
			remoteObject.source = "RemoteServiceFormation";
			remoteObject.endpoint = ApplicationFacade.SERVEUR;
		} 
 	
 		public function obtenirListeFormations() : void 
 		{
 			remoteObject.addEventListener(ResultEvent.RESULT, listeFormationsObtenue);
			remoteObject.addEventListener(FaultEvent.FAULT, onFault);
 			remoteObject.obtenirListeFormations();
 		}
 		
 		public function validerAjoutFormation(formation:Formation):void
		{
			remoteObject.addEventListener(ResultEvent.RESULT, listeFormationsObtenue);
			remoteObject.addEventListener(FaultEvent.FAULT, onFault);
 			remoteObject.ajouterFormation(formation);
		}
		
		public function validerModifFormation(formation:Formation):void
		{
			remoteObject.addEventListener(ResultEvent.RESULT, listeFormationsObtenue);
			remoteObject.addEventListener(FaultEvent.FAULT, onFault);
 			remoteObject.modifierFormation(formation);
		}
		
		public function supprimerFormation(listeFormations:Array):void
		{
			remoteObject.addEventListener(ResultEvent.RESULT, listeFormationsObtenue);
			remoteObject.addEventListener(FaultEvent.FAULT, onFault);
 			remoteObject.supprimerFormation(listeFormations);
		}
		 
		private function listeFormationsObtenue(event:ResultEvent):void
		{
			var listeFormations:Array = (event.result as Array)
			sendNotification(LISTE_FORMATIONS_OBTENUE, listeFormations);
		}
		
		
		private function onFault(event:FaultEvent):void
		{
			trace("Fault");			
		}
		
		private function listeFormationsSupprimee(event:ResultEvent):void
		{
			var listeFormations:Array = (event.result as Array)
			sendNotification(LISTE_FORMATIONS_OBTENUE, listeFormations);
		}
		
	}
}